﻿using NetEasyMusic.Lib.Models;
using NetEasyMusic.Lib.Recommend;
using Newtonsoft.Json.Linq;

namespace NetEasyMusic.Lib.Data;

public class Video : DataBasic
{
    public Video(string cookie) : base(cookie)
    {
    }

    public Video()
    {
    }

    public Video(string url, bool isUse = false) : base(url, isUse)
    {
    }

    public async Task<string> GetVideoUrl(string id)
    {
        return await SharedClient.GetStringAsync($"/video/url?id={id}");
    }

    /// <summary>
    ///     mv 排行
    /// </summary>
    /// <returns></returns>
    public async Task<TopMVRecommend[]> TopMV()
    {
        var context = await GetFormString("/top/mv");
        return context?["data"]?.Select(x => new TopMVRecommend(x)).ToArray() ?? [];
    }

    /// <summary>
    ///     推荐 mv
    /// </summary>
    /// <returns></returns>
    public async Task<MVRecommend[]> MV()
    {
        var context = await GetFormString("/personalized/mv");
        if (context == null) return [];
        var array = context["result"]!;
        return array.Select(x => new MVRecommend(x)).ToArray();
    }

    /// <summary>
    ///     获取推荐视频
    /// </summary>
    /// <returns></returns>
    public async Task<VideoRecommend> VideoTask()
    {
        var result = await SharedClient.GetStringAsync($"/video/timeline/recommend?cookie={Cookie}");
        var context = JObject.Parse(result);
        return new VideoRecommend(context);
    }
    
    public async Task<BannerModel[]> Banner()
    {
        var context = await GetFormString("/dj/banner");
        if (context == null) return [];
        var array = context["data"]!;
        return array.Select(x => new BannerModel()
        {
            ImageUrl = x["pic"]?.ToObject<string>()!,
            Id = x["targetId"]?.ToObject<string>()!,
            Title = x["typeTitle"]?.ToObject<string>()!
        }).ToArray();
    }
}